package com.javaee.wordtree.controller;

import com.javaee.wordtree.common.Result;
import com.javaee.wordtree.entity.ModelChat;
import com.javaee.wordtree.entity.ModelMessage;
import com.javaee.wordtree.service.ModelChatService;
import com.javaee.wordtree.service.ModelMessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 英语对话控制器
 */
@RestController
@RequestMapping("/english-chat")
public class EnglishChatController {

    @Resource
    private ModelChatService modelChatService;

    @Resource
    private ModelMessageService modelMessageService;

    private static final Logger logger = LoggerFactory.getLogger(EnglishChatController.class);

    /**
     * 创建新的英语对话
     * 
     * @param params 请求参数
     * @return 创建的聊天会话
     */
    @PostMapping
    public Result<?> createEnglishChat(@RequestBody Map<String, String> params, HttpServletRequest request) {
        String userId = getCurrentUserId(request);
        String title = params.getOrDefault("title", "英语对话");
        String difficulty = params.getOrDefault("difficulty", "medium");
        String keywords = params.getOrDefault("keywords", "");

        // 创建英语对话会话
        ModelChat chat = modelChatService.createChat(userId, title, "english", difficulty, keywords);

        // 如果有初始消息，则发送
        String initialMessage = params.get("initialMessage");
        if (initialMessage != null && !initialMessage.isEmpty()) {
            ModelMessage aiReply = modelMessageService.saveMessageAndGenerateReply(chat.getChatId(), initialMessage);

            // 创建包含聊天和回复的响应
            Map<String, Object> response = new HashMap<>();
            response.put("chat", chat);
            response.put("reply", aiReply);

            return Result.success(response);
        }

        return Result.success(chat);
    }

    /**
     * 获取英语对话设置选项
     * 
     * @return 可用的设置选项
     */
    @GetMapping("/options")
    public Result<?> getEnglishChatOptions() {
        Map<String, Object> options = new HashMap<>();

        // 难度选项
        Map<String, String> difficulties = new HashMap<>();
        difficulties.put("easy", "初级 - 简单词汇和句型");
        difficulties.put("medium", "中级 - 中等词汇和句型");
        difficulties.put("hard", "高级 - 复杂词汇和句型");
        options.put("difficulties", difficulties);

        // 一些预定义的主题建议
        String[] topicSuggestions = {
                "日常生活", "旅行", "食物", "教育", "工作", "技术", "环境", "文化",
                "电影", "音乐", "体育", "健康", "科学"
        };
        options.put("topicSuggestions", topicSuggestions);

        // 一些预定义的开场白
        String[] greetings = {
                "Hi there! How are you today?",
                "Hello! What would you like to talk about?",
                "Good day! How can I help you practice English?",
                "Hey! I'm here to chat with you in English. What's on your mind?"
        };
        options.put("greetings", greetings);

        return Result.success(options);
    }

    /**
     * 获取当前用户ID（实际应用中应该从JWT或session中获取）
     */
    private String getCurrentUserId(HttpServletRequest request) {
        // 这里简单模拟从请求头中获取用户ID
        String userId = request.getHeader("X-User-ID");
        // 临时使用固定用户ID进行开发测试
        return userId != null ? userId : "123456";
    }
}
